package com.example.chuntiao.cophook.ui;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.example.chuntiao.cophook.R;
import com.example.chuntiao.cophook.mytools.HttpTool;

import java.io.IOException;

import static android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP;

public class InputCheckNumActivity extends AppCompatActivity {

    private EditText editCheck;
    private EditText editPass;
    private EditText editPass2;
    private TextWatcher watcher;
    private LinearLayout layoutDone;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input_check_num);

        watcher=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if("".equals(editCheck.getText().toString())
                        || !editPass.getText().toString().equals(editPass2.getText().toString()))
                    layoutDone.setEnabled(false);
                else
                    layoutDone.setEnabled(true);
            }
        };



        editCheck=(EditText)findViewById(R.id.edit_check_num);
        editPass=(EditText)findViewById(R.id.edit_pass);
        editPass2=(EditText)findViewById(R.id.edit_pass2);
        layoutDone=(LinearLayout)findViewById(R.id.layout_done);

        editCheck.addTextChangedListener(watcher);
        editPass.addTextChangedListener(watcher);
        editPass2.addTextChangedListener(watcher);



        layoutDone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AsyncTask<Void,Void,Void>(){
                    ProgressDialog progressDialog=null;
                    AlertDialog alertDialog=null;
                    String checkNum=editCheck.getText().toString().trim();
                    String pass=editPass.getText().toString().trim();
                    boolean succ=true;
                    @Override
                    protected void onPreExecute() {
                        progressDialog=ProgressDialog.show(InputCheckNumActivity.this,"稍等","正在发送修改请求");
                        alertDialog = new AlertDialog.Builder(InputCheckNumActivity.this)
                                .setMessage("修改失败，未知错误。")
                                .setPositiveButton(R.string.yse, null)
                                .create();
                    }

                    @Override
                    protected Void doInBackground(Void... params) {
                        try {
                            succ=HttpTool.updatePass(InputCheckNumActivity.this,
                                    checkNum,pass);

                            if(!succ)
                                alertDialog.setMessage("验证码错误");
                        } catch (IOException e) {
                            succ=false;
                            e.printStackTrace();
                        }
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void aVoid) {
                        progressDialog.dismiss();
                        if(!succ)
                            alertDialog.show();
                        else{
                            alertDialog=null;
                            Intent intent=new Intent(getApplicationContext(),LogInActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }
                }.execute();
            }
        });


    }
}
